package com.android.jingweiapp.adapter;

import android.content.Context;
import android.os.Parcelable;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.android.jingweiapp.R;
import com.android.jingweiapp.bean.HotWord;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by cuiwenrui-pc on 15/11/23.
 */
public class HotWordAdapter extends RecyclerView.Adapter<HotWordAdapter.NormalTextViewHolder> implements View.OnClickListener {

    private final LayoutInflater mLayoutInflater;
    private ArrayList<HotWord.DataEntity> beans;
    private OnRecyclerViewItemClickListener mOnItemClickListener = null;

    public HotWordAdapter(Context context, List<HotWord.DataEntity> beans) {
        this.beans = (ArrayList<HotWord.DataEntity>) beans;
        mLayoutInflater = LayoutInflater.from(context);
    }

    @Override
    public HotWordAdapter.NormalTextViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = mLayoutInflater.inflate(R.layout.item_hotword, parent, false);
        return new NormalTextViewHolder(view);
    }

    @Override
    public void onBindViewHolder(NormalTextViewHolder holder, int position) {
        holder.tvHotWord.setText(beans.get(position).getContent());
        holder.tvHotWord.setTag(beans.get(position));
        holder.tvHotWord.setOnClickListener(this);
    }


    @Override
    public int getItemCount() {
        return beans == null ? 0 : beans.size();
    }

    public void setOnItemClickListener(OnRecyclerViewItemClickListener listener) {
        this.mOnItemClickListener = listener;
    }

    @Override
    public void onClick(View v) {
        mOnItemClickListener.onItemClick(v, (Parcelable) v.getTag());
    }

    public interface OnRecyclerViewItemClickListener {
        void onItemClick(View view, Parcelable data);
    }


    public static class NormalTextViewHolder extends RecyclerView.ViewHolder {
        @Bind(R.id.tv_hotWord)
        TextView tvHotWord;

        NormalTextViewHolder(View view) {
            super(view);
            ButterKnife.bind(this, view);

        }
    }
}
